package com.prs.crm.action.fs;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.fs.AcctType;
import com.prs.crm.service.fs.AcctTypeService;
import com.prs.crm.service.sys.LogService;

public class AcctTypeAction extends BaseAction {
	private AcctType acctType;
	private AcctTypeService acctTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listAcctType")
	public String listAcctType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				AcctType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAcctTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editAcctType")
	public String editAcctType() {
		return SUCCESS;
	}

	@Action(value = "submitAcctType", results = {
			@Result(name = "success", type = "redirect", location = "listAcctType"),
			@Result(name = "error", type = "chain", location = "editAcctType") })
	public String submitAcctType() {
		if (acctType.getName() == null || "".equals(acctType.getName().trim())) {
			addActionError("类别不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(AcctType.class, acctType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getAcctTypeService().save(acctType);
		return SUCCESS;
	}

	@Action(value = "removeAcctType", results = {
			@Result(name = "success", type = "redirect", location = "listAcctType"),
			@Result(name = "error", type = "chain", location = "editAcctType") })
	public String removeAcctType() {
		for (Integer id : ids) {
			AcctType type = this.getAcctTypeService().get(id);
			if (type.getAccts().size() != 0) {
				this.addActionError("已有会计科目引用不能删除");
				return ERROR;
			}
		}
		this.getAcctTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public AcctType getAcctType() {
		return acctType;
	}

	public void setAcctType(AcctType acctType) {
		this.acctType = acctType;
	}

	public AcctTypeService getAcctTypeService() {
		return acctTypeService;
	}

	public void setAcctTypeService(AcctTypeService acctTypeService) {
		this.acctTypeService = acctTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
